package com.kudian.restful.service;

import com.kudian.common.persistence.HibernateDao;
import com.kudian.common.service.BaseService;
import com.kudian.common.utils.DateUtils;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.dao.*;
import com.kudian.restful.entity.*;
import com.kudian.restful.vo.ad.AdItemVO;
import com.kudian.restful.vo.ad.MidAdRetVO;
import com.kudian.restful.vo.ad.MidAdVO;
import com.kudian.restful.vo.collection.CollectionGoodsItemVO;
import com.kudian.restful.vo.collection.CollectionSellerItemVO;
import com.kudian.restful.vo.collection.QueryCollectionVO;
import com.kudian.restful.vo.comment.CommentInfoVO;
import com.kudian.restful.vo.goods.GoodsItemVO;
import com.kudian.restful.vo.seller.SellerItemVO;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/18.
 */
@Service
@Transactional(readOnly = true)
public class CollectionService extends BaseService<Collection, Integer> {
    private static final Logger logger = Logger.getLogger(CollectionService.class);

    @Autowired
    private CollectionDao collectionDao;

    @Autowired
    private LoginSessionDao loginSessionDao;

    @Autowired
    private SellerDao sellerDao;

    @Autowired
    private MediaResService mediaResService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private OrderService orderService;

    @Override
    public HibernateDao<Collection, Integer> getEntityDao() {
        return collectionDao;
    }

    public List<CollectionSellerItemVO> queryCollectionSellers(QueryCollectionVO vo) {
        List<CollectionSellerItemVO> items = null;
        LoginSession ent = this.loginSessionDao.get(vo.getAccess_token());
        if (ent == null) {
            return null;
        }

        List<Collection> cols = this.collectionDao.findbyorder("addTime", false,
                Restrictions.eq("userId", ent.getUserId()),
                Restrictions.ne("sellerId", 0), Restrictions.eq("goodsId", 0));

        for (Collection col : cols) {
            if (items == null) {
                items = new ArrayList<CollectionSellerItemVO>();
            }

            CollectionSellerItemVO item = new CollectionSellerItemVO();
            Seller se = this.sellerDao.get(col.getSellerId());

            item.setCollectionId(col.getCollectionId());
            item.setIsCollect(true);
            item.setSellerId(se.getSellerId());
            item.setGtId(se.getGtId());
            item.setName(se.getName());
            item.setTel(se.getTel());
            // 配送时间
            item.setDeliveryTime(se.getDeliveryTime());
            item.setMobilePhone(se.getMobilePhone());
            item.setUserId(se.getUserId());
            // TODO 是否返回用户信息

            item.setAddress(se.getAddress());
            if (se.getRegTime() != null) {
                item.setRegTime(DateUtils.dateFormat(se.getRegTime()));
            }
            if (se.getModifyTime() != null) {
                item.setModifyTime(DateUtils.dateFormat(se.getRegTime()));
            }
            item.setCorporation(se.getCorporation());
            item.setMinChange(se.getMinChange());
            item.setIsFirst(se.getIsFirst());
            item.setFirstAmount(se.getFirstAmount());
            item.setIsFullMinus(se.getIsFullMinus());
            item.setFullAmount(se.getFullAmount());
            item.setMinusAmount(se.getMinusAmount());
            item.setIsShippingFree(se.getIsShippingFree());
            item.setShippingFee(se.getShippingFee());

            item.setIsHoliday(se.getIsHoliday());
            item.setDiscount(se.getDiscount());

            item.setBusLicencePics(mediaResService.getResUrls(se.getBusLicencePics()));
            item.setCorporateIcardPics(mediaResService.getResUrls(se.getCorporateIcardPics()));
            item.setSellerPics(mediaResService.getResUrls(se.getSellerPics()));

            // 前台转译
            item.setSellerStatus(se.getSellerStatus());

            item.setDeliveryStart(se.getDeliveryStart());
            item.setDeliveryEnd(se.getDeliveryEnd());
            item.setIsopen(se.getIsopen());

            //item.setLatitudes(se.getLatitudes());
            //item.setLongitudes(se.getLongitudes());

            CommentInfoVO cinfo = commentService.queryBySeller(se.getSellerId());
            if (cinfo != null) {
                // 取平均值
                item.setCommentRank(cinfo.getCommentRank());
                // 评论数
                item.setCommentCount(cinfo.getCommentCount());
            } else {
                // 取平均值
                item.setCommentRank((short)0);
                // 评论数
                item.setCommentCount(0);
            }
            // 取月销量
            item.setSales(orderService.getOrdersNumBySeller(se.getSellerId()));
            // TODO 需要位置转换
            item.setDistance("0");

            items.add(item);
        }

        return items;
    }

    public List<CollectionGoodsItemVO> queryCollectionGoods(QueryCollectionVO vo) {
        List<CollectionGoodsItemVO> items = null;
        LoginSession ent = this.loginSessionDao.get(vo.getAccess_token());
        if (ent == null) {
            return null;
        }

        List<Collection> cols = this.collectionDao.findbyorder("addTime", false,
                Restrictions.eq("userId", ent.getUserId()),
                Restrictions.eq("sellerId", 0), Restrictions.ne("goodsId", 0));

        for (Collection col : cols) {

            if (items == null)
                items = new ArrayList<CollectionGoodsItemVO>();

            GoodsItemVO v = goodsService.details(col.getGoodsId());
            CollectionGoodsItemVO item = new CollectionGoodsItemVO();
            BeanUtils.copyProperties(v, item);

            Seller s = sellerDao.get(v.getSellerId());
            if (s != null) {
                item.setSellerName(s.getName());
                item.setMinChange(s.getMinChange());
                item.setShippingFee(s.getShippingFee());
                item.setIsFirst(s.getIsFirst());
                item.setFirstAmount(s.getFirstAmount());
                item.setIsFullMinus(s.getIsFullMinus());
                item.setFullAmount(s.getFullAmount());
                item.setMinusAmount(s.getMinusAmount());
                item.setIsShippingFree(s.getIsShippingFree());
            }

            CommentInfoVO cinfo = commentService.queryByGoods(col.getGoodsId());
            if (cinfo != null) {
                // 取平均值
                item.setCommentRank(cinfo.getCommentRank());
                // 评论数
                item.setCommentCount(cinfo.getCommentCount());
            } else {
                // 取平均值
                item.setCommentRank((short)0);
                // 评论数
                item.setCommentCount(0);
            }
            // 取月销量
            item.setSoldnum(orderService.getNumByGoods(col.getGoodsId()));
            item.setIsCollect(true);
            item.setCollectionId(col.getCollectionId());
            // TODO 需要位置转换
            item.setDistance("0");
            items.add(item);
        }
        return items;
    }
}
